Also, playfields have convenience methods to perform tasks such as fetching images from the web server.
// Be sure to import the baklava package import baklava.*; // A typical applet class declaration public class MyApplet extends Applet { // A reference to the playfield Playfield p; // The URL from which we will fetch images // CHANGE THIS, of course! String base = "http://www.mysite.com/graphics/"; // The web browser calls init when it is ready for the // applet to start operating public void init() { // Use no layout manager setLayout(null); // Pass a reference to the applet, as well as // the width and height of the applet p = new Playfield(this, bounds().width, bounds().height); // Create a simple sprite Sprite s = new Sprite(p); // Tell the sprite to bounce at the edges of the playfield s.setEdgeHandling(Sprite.edgeBounce); // Fetch an image Image image = p.getImage(base + "wanderer.gif"); // Set this as the image for the sprite s.setImage(image); // Set a direction (45 degrees) s.setDirection(45); // Start it moving: 30 pixels per second s.setSpeed(30); // Now start movement in the playfield p.start(); // Now display the playfield add(p); } // When the user leaves the web page public void stop() { p.suspend(); } // When the user comes back public void start() { p.resume(); } // When the browser wants the applet to go away public void destroy() { p.stop(); } };
import baklava.*; class MyApplet extends Applet { public void init() { setLayout(null); Playfield p = new Playfield(this, bounds().width, bounds().height); // Create various sprites... // Now start movement in the playfield p.start(); } };
import baklava.*; // Be sure to specify that we implement the GlobalTimerObserver interface class MyApplet extends Applet implements GlobalTimerObserver { static final int helloTimer = 1; Playfield p; public void init() { setLayout(null); p = new Playfield(this, bounds().width, bounds().height); // This is valid because we implement GlobalTimerObserver p.setGlobalTimerObserver(this); // Send a helloTimer event in 1 second (1000 milliseconds) setGlobalTimer(1000, helloTimer); // Create various sprites... // Now start movement in the playfield p.start(); } public void globalTimer(int timerId) { // This will print 1 in the java console. system.out.println(timerId); // Now set the timer again. setGlobalTimer(1000, helloTimer); } };
delay
milliseconds (thousands of
a second). The timerId
argument will be passed
to the globalTimer method of the global timer observer. For more
information, and an example, see the discussion of the
setGlobalTimerObserver method.
See also Sprite.setTimer for a
way to send a timer event to an individual sprite.
// Dismiss only sprites of the Scone subclass. void dismissScones(Playfield p) { p.goodbyeAll(Class.forName("Scone")); }
// Tell all of the scones to move left one second from now. void dismissScones(Playfield p) { p.setTimerAll(Class.forName("Scone"), 1000, Scone.timerLeft); }
true
.
To restore the default behavior, invoke setShowProgress with
an argument of false
.
Loading Images
. You can change this text
by calling the setProgressMessage method with your preferred text.